package com.hyp.pay.UI.Zxing.presenter;

import android.util.Log;

import com.hyp.pay.UI.Zxing.presenter.contract.TestScanContract;
import com.hyp.pay.base.RxPresenter;
import com.hyp.pay.http.Constants;
import com.hyp.pay.http.RetrofitHelper;
import com.hyp.pay.model.JsonModel;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.subscribers.ResourceSubscriber;

/**
 * Created by Administrator on 2017/6/13.
 */

public class TestScanPresenter   extends RxPresenter<TestScanContract.View> implements TestScanContract.Presenter{

    private RetrofitHelper mRetrofitHelper;

    @Inject
    public TestScanPresenter(RetrofitHelper mRetrofitHelper) {
        this.mRetrofitHelper = mRetrofitHelper;
    }

    @Override
    public void getWeixin(String goodsname,String Charge, String CreatTime, String Type, String UserId, String AuthCode, String AddIp) {
        Flowable<String> flowable = mRetrofitHelper.fetchWeixin(goodsname,Charge,CreatTime,Type,UserId,AuthCode,AddIp);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView()!=null){
                    getView().showWeixin(data);
                }

//                if (getView() != null) {
//                    if (data.getResult_code().equals(Constants.Weixin_SUCCESS)) {
//                        getView().showWeixin(data);
//                    } else {
//                        getView().showError(data.getReturn_msg());
//                    }
//                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getZhiPay(String goods_name,String Charge, String CreatTime, String PayType, String UserId, String AuthCode, String AddIp) {
        Flowable<String> flowable = mRetrofitHelper.fetchZhiPay(goods_name,Charge,CreatTime,PayType,UserId,AuthCode,AddIp);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showZhiPay(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getBaiDu(String goods_name,String total_amount, String pay_type, String addip, String pay_code,String userid) {
        Flowable<String> flowable = mRetrofitHelper.fetchBaiduPay(goods_name,total_amount,pay_type,addip,pay_code,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showBaiDu(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getJDPay(String receiverName, String amount, String bankName, String receiverCardNo, String bankCode, String AddIp) {
        Flowable<JsonModel> flowable = mRetrofitHelper.fetchJDPay(receiverName,amount,bankName,receiverCardNo,bankCode,AddIp);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<JsonModel>() {

            @Override
            public void onNext(JsonModel data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    if (data.getReturn_msg().equals(Constants.JD_SUCCESS)) {
                        getView().showJDPay(data);
                    } else {
                        getView().showError(data.getReturn_msg());
                    }
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getQQPay(String totalFee, String AuthCode, String AddIp,String userid) {
        Flowable<String> flowable = mRetrofitHelper.fetchQQPay(totalFee,AuthCode,AddIp,userid);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showQQPay(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getWeixinOrder(String order) {
        Flowable<String> flowable = mRetrofitHelper.fetchWeinxinOrder(order);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showWeixinOrder(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getZhifuOrder(String order) {
        Flowable<String> flowable = mRetrofitHelper.fetchzhifuOrder(order);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showZhifuOrder(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getBaiduOrder(String order) {
        Flowable<String> flowable = mRetrofitHelper.fetchBaiduOrder(order);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showBaiduOrder(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getQQOrder(String order, String ip) {
        Flowable<String> flowable = mRetrofitHelper.fetchQQOrder(order,ip);
        ResourceSubscriber subscriber = mRetrofitHelper.startObservable(flowable, new ResourceSubscriber<String>() {

            @Override
            public void onNext(String data) {
                Log.i("olj",data.toString());
                if (getView() != null) {
                    getView().showQQOrder(data);
                }
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                if (getView() != null) {
//                    getView().showError(Constants.GRS_ERROR);
                }
            }

            @Override
            public void onComplete() {

            }
        });
        addSubscription(subscriber);
    }

    @Override
    public void getTimber() {
        //从1开始输出10个数据，延迟0秒执行，每隔5秒执行一次：
        Flowable<Long> flowable = Flowable.intervalRange(1, 10, 0, 5, TimeUnit.SECONDS);

        ResourceSubscriber observer = new ResourceSubscriber<Long>() {
            @Override
            public void onNext(Long time) {
                if (getView() != null)
                    getView().showTimber(time);
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }

        };

        addSubscription(mRetrofitHelper.startObservable(flowable, observer));
    }



}
